package fw.asmx.materialdesign.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.Switch;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fw.asmx.materialdesign.R;
import fw.asmx.materialdesign.common.consts.PrefType;
import fw.asmx.materialdesign.common.item.PrefListItem;

public class RecyclerViewPreferenceFragment extends Fragment
{
    private RecyclerView rv;
    private List<PrefListItem> lst;

    private OnItemClickListener onItemClickListener;

    public RecyclerViewPreferenceFragment()
    {
        lst = new ArrayList<>();
    }

    @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                                 Bundle savedInstanceState)
    {
        lst.add(new PrefListItem(PrefType.SECTION, true, "Font", null));
        View v = inflater.inflate(R.layout.common_recyclerview, container, false);
        rv = (RecyclerView) v;
        rv.setLayoutManager(new LinearLayoutManager(container.getContext()));
        rv.setAdapter(new RecyclerView.Adapter()
        {
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
            {
                View v;
                switch (viewType)
                {
                    case PrefType.SECTION:
                        v = new TextView(parent.getContext());
                        return new ViewHolderSectionTitle(v);
                    case PrefType.TEXT:
                        v = LayoutInflater.from(parent.getContext())
                                          .inflate(R.layout.item_fragment_rv_preference_list_text,
                                                   parent, false);
                        return new ViewHolderTextPreference(v);
                    case PrefType.CHECKBOX:
                        v = LayoutInflater.from(parent.getContext())
                                          .inflate(
                                                  R.layout.item_fragment_rv_preference_list_checkbox,
                                                  parent, false);
                        return new ViewHolderCheckBoxPreference(v);
                    case PrefType.SWITCH:
                        v = LayoutInflater.from(parent.getContext())
                                          .inflate(R.layout.item_fragment_rv_preference_list_switch,
                                                   parent, false);
                        return new ViewHolderSwitchPreference(v);
                    default:
                        return null;
                }
            }

            @Override public void onBindViewHolder(RecyclerView.ViewHolder vh, int position)
            {
                Map<String, Object> ext = lst.get(position).extInf;
                if (vh instanceof ViewHolderSectionTitle)
                {
                    ((ViewHolderSectionTitle) vh).title.setText(lst.get(position).prefTitle);
                }
                else if (vh instanceof ViewHolderTextPreference)
                {
                    ((ViewHolderTextPreference) vh).title.setText(lst.get(position).prefTitle);
                    ((ViewHolderTextPreference) vh).summary.setText(ext.get("Summary").toString());
                }
                else if (vh instanceof ViewHolderCheckBoxPreference)
                {
                    ((ViewHolderCheckBoxPreference) vh).title.setText(lst.get(position).prefTitle);
                    ((ViewHolderCheckBoxPreference) vh).summary
                            .setText(ext.get("Summary").toString());
                    ((ViewHolderCheckBoxPreference) vh).cb.setChecked(lst.get(position).isEnabled);
                }
                else if (vh instanceof ViewHolderSwitchPreference)
                {
                    ((ViewHolderSwitchPreference) vh).title.setText(lst.get(position).prefTitle);
                    ((ViewHolderSwitchPreference) vh).summary
                            .setText(ext.get("Summary").toString());
                }
            }

            @Override public int getItemViewType(int position)
            {
                return lst.get(position).prefType;
            }

            @Override public int getItemCount()
            {
                return lst.size();
            }
        });
        return v;
    }

    class ViewHolderSectionTitle extends RecyclerView.ViewHolder
    {
        public TextView title;

        public ViewHolderSectionTitle(View v)
        {
            super(v);
            title = (TextView) v;
        }
    }

    class ViewHolderCheckBoxPreference extends RecyclerView.ViewHolder
            implements View.OnClickListener
    {
        public CheckBox cb;
        public TextView title;
        public TextView summary;

        public ViewHolderCheckBoxPreference(View v)
        {
            super(v);
            cb = (CheckBox) v.findViewById(R.id.cb);
            title = (TextView) v.findViewById(R.id.title);
            summary = (TextView) v.findViewById(R.id.summary);

            v.setOnClickListener(this);
        }

        @Override public void onClick(View v)
        {
            if (onItemClickListener != null)
            {
                onItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

    class ViewHolderTextPreference extends RecyclerView.ViewHolder
            implements View.OnClickListener
    {
        public TextView title;
        public TextView summary;

        public ViewHolderTextPreference(View v)
        {
            super(v);
            title = (TextView) v.findViewById(R.id.title);
            summary = (TextView) v.findViewById(R.id.summary);

            v.setOnClickListener(this);
        }

        @Override public void onClick(View v)
        {
            if (onItemClickListener != null)
            {
                onItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

    class ViewHolderSwitchPreference extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        public Switch aSwitch;
        public TextView title;
        public TextView summary;

        public ViewHolderSwitchPreference(View v)
        {
            super(v);
            aSwitch = (Switch) v.findViewById(R.id.a_switch);
            title = (TextView) v.findViewById(R.id.title);
            summary = (TextView) v.findViewById(R.id.summary);

            v.setOnClickListener(this);
        }

        @Override public void onClick(View v)
        {
            if (onItemClickListener != null)
            {
                onItemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }

    public void addSection(String title)
    {
        lst.add(new PrefListItem(PrefType.SECTION, true, title, null));
    }

    public void addTextPrefItem(String title, String tip, boolean isEnable)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("Summary", tip);
        lst.add(new PrefListItem(PrefType.TEXT, isEnable, title, map));
    }

    public void addCheckBoxPrefItem(String title, String tip, String prefKey, boolean checkState,
                                    boolean isEnable)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("Key", prefKey);
        map.put("Summary", tip);
        map.put("CheckState", checkState);
        lst.add(new PrefListItem(PrefType.CHECKBOX, isEnable, title, map));
    }

    public void addSwitchPrefItem(String title, String tip, String prefKey, boolean switchState,
                                  boolean isEnable)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("Key", prefKey);
        map.put("Summary", tip);
        map.put("SwitchState", switchState);
        lst.add(new PrefListItem(PrefType.SWITCH, isEnable, title, map));
    }

    public void refreshPrefList()
    {
        rv.getAdapter().notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener)
    {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener
    {
        void onItemClick(View v, int pos);
    }
}
